package sy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.BaseDaoI;
import sy.model.TsupplierInfo;
import sy.model.TsupplierTvehicle;
import sy.model.TvehicleInfo;
import sy.pageModel.DataGrid;
import sy.pageModel.SupplierInfo;
import sy.service.SupplierInfoServiceI;

@Service("supplierInfoService")
public class SupplierInfoServiceImpl implements SupplierInfoServiceI {

	private static final Logger logger = Logger.getLogger(SupplierInfoServiceImpl.class);

	private BaseDaoI<TsupplierInfo> supplierInfoDao;
	private BaseDaoI<TvehicleInfo> vehicleDao;
	private BaseDaoI<TsupplierTvehicle> supplierVehicleDao;

	public BaseDaoI<TsupplierInfo> getSupplierInfoDao() {
		return supplierInfoDao;
	}

	@Autowired
	public void setSupplierInfoDao(BaseDaoI<TsupplierInfo> supplierInfoDao) {
		this.supplierInfoDao = supplierInfoDao;
	}

	
	public BaseDaoI<TvehicleInfo> getVehicleDao()
    {
        return vehicleDao;
    }
	@Autowired
    public void setVehicleDao(BaseDaoI<TvehicleInfo> vehicleDao)
    {
        this.vehicleDao = vehicleDao;
    }
    
    
    public BaseDaoI<TsupplierTvehicle> getSupplierVehicleDao()
    {
        return supplierVehicleDao;
    }
    @Autowired
    public void setSupplierVehicleDao(BaseDaoI<TsupplierTvehicle> supplierVehicleDao)
    {
        this.supplierVehicleDao = supplierVehicleDao;
    }

    public SupplierInfo save(SupplierInfo supplierInfo) {
		TsupplierInfo t = new TsupplierInfo();
		
		BeanUtils.copyProperties(supplierInfo, t);
		t.setSupplierid(UUID.randomUUID().toString());
		supplierInfoDao.save(t);
		if (supplierInfo.getVehicleIds()!= null) {
			String vehicleIds = "";
			String tons = "";
			for (String id : supplierInfo.getVehicleIds().split(",")) {
				TvehicleInfo v = vehicleDao.get(TvehicleInfo.class, id);
				if (v != null) {
					TsupplierTvehicle sv = new TsupplierTvehicle();
					sv.setId(UUID.randomUUID().toString());
					sv.setTvehicle(v);
					sv.setTsupplier(t);
					supplierVehicleDao.save(sv);
					if (vehicleIds.length() > 0) {
					    vehicleIds+= ",";
						tons += ",";
					}
					vehicleIds += v.getVehicleid();
					tons += v.getTon();
				}
			}
			supplierInfo.setVehicleIds(vehicleIds);
			supplierInfo.setTons(tons);
		}
		BeanUtils.copyProperties(t, supplierInfo);
		return supplierInfo;
	}

	
	public SupplierInfo find(SupplierInfo SupplierInfo) {
	/*	Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", SupplierInfo.getName());
		params.put("pwd", Encrypt.e(SupplierInfo.getPwd()));
		TSupplierInfo t = SupplierInfoDao.get("from TSupplierInfo t where t.name=:name and t.pwd=:pwd", params);
		if (t != null) {
			BeanUtils.copyProperties(t, SupplierInfo);

			String roleIds = "";
			String roleNames = "";
			String resourceIds = "";
			String resourceNames = "";
			List<String> resourceUrls = new ArrayList<String>();
			Set<TSupplierInfoTrole> ur = t.getTSupplierInfoTroles();// 获得当前用户的所有角色
			if (ur != null && !ur.isEmpty()) {
				for (TSupplierInfoTrole tur : ur) {
					if (roleIds.length() > 0) {
						roleIds += ",";
						roleNames += ",";
					}
					roleIds += tur.getTrole().getId();
					roleNames += tur.getTrole().getText();

					Set<TroleTresource> rr = tur.getTrole().getTroleTresources();// 获得当前角色所有资源
					if (rr != null && !rr.isEmpty()) {
						for (TroleTresource trr : rr) {
							if (resourceIds.length() > 0) {
								resourceIds += ",";
								resourceNames += ",";
							}
							resourceIds += trr.getTresource().getId();
							resourceNames += trr.getTresource().getText();
							resourceUrls.add(trr.getTresource().getUrl());
						}
					}
				}
			}
			SupplierInfo.setRoleIds(roleIds);
			SupplierInfo.setRoleNames(roleNames);
			SupplierInfo.setResourceIds(resourceIds);
			SupplierInfo.setResourceNames(resourceNames);
			SupplierInfo.setResourceUrls(resourceUrls);

			return SupplierInfo;
		}*/
		return null;
	}

	
	public DataGrid datagrid(SupplierInfo supplierInfo) {
		DataGrid dg = new DataGrid();
		String hql = "from TsupplierInfo t ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(supplierInfo, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(supplierInfo, hql);
		List<TsupplierInfo> l = supplierInfoDao.find(hql, params, supplierInfo.getPage(), supplierInfo.getRows());
		List<SupplierInfo> nl = new ArrayList<SupplierInfo>();
		changeModel(l, nl);
		dg.setTotal(supplierInfoDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<TsupplierInfo> l, List<SupplierInfo> nl) {
		if (l != null && l.size() > 0) {
			for (TsupplierInfo t : l) {
				SupplierInfo supplierInfo = new SupplierInfo();
				BeanUtils.copyProperties(t, supplierInfo);

				Set<TsupplierTvehicle> s = t.getTsupplierTvehicles();
				if (s != null && !s.isEmpty()) {
					String vehicleIds = "";
					String tons = "";

					for (TsupplierTvehicle tt : s) {
						if (vehicleIds.length() > 0) {
						    vehicleIds += ",";
							tons += ",";
						}
						vehicleIds += tt.getTvehicle().getVehicleid();
						tons+= tt.getTvehicle().getTon();
					}

					supplierInfo.setVehicleIds(vehicleIds);
					supplierInfo.setTons(tons);
				}

				nl.add(supplierInfo);
			}
		}
	}

	private String addOrder(SupplierInfo supplierInfo, String hql) {
		if (supplierInfo.getSort() != null) {
			hql += " order by " + supplierInfo.getSort() + " " + supplierInfo.getOrder();
		}
		return hql;
	}

	private String addWhere(SupplierInfo supplierInfo, String hql, Map<String, Object> params) {
		hql += " where 1=1 ";
		/*if (SupplierInfo.getCreatedatetimeStart() != null) {
			hql += " and t.createdatetime >= :getCreatedatetimeStart";
			params.put("getCreatedatetimeStart", SupplierInfo.getCreatedatetimeStart());
		}
		if (SupplierInfo.getCreatedatetimeEnd() != null) {
			hql += " and t.createdatetime <= :getCreatedatetimeEnd";
			params.put("getCreatedatetimeEnd", SupplierInfo.getCreatedatetimeEnd());
		}
		if (SupplierInfo.getModifydatetimeStart() != null) {
			hql += " and t.modifydatetime >= :getModifydatetimeStart";
			params.put("getModifydatetimeStart", SupplierInfo.getModifydatetimeStart());
		}
		if (SupplierInfo.getModifydatetimeEnd() != null) {
			hql += " and t.modifydatetime <= :getModifydatetimeEnd";
			params.put("getModifydatetimeEnd", SupplierInfo.getModifydatetimeEnd());
		}
		if (SupplierInfo.getQ() != null && !SupplierInfo.getQ().trim().equals("")) {
			hql += " and t.name like :name ";
			params.put("name", "%%" + SupplierInfo.getQ().trim() + "%%");
		}*/
		if (supplierInfo.getSupplierzhname() != null && !supplierInfo.getSupplierzhname().trim().equals("")) {
			hql += " and t.supplierzhname like :supplierzhname";
			params.put("supplierzhname", "%%" + supplierInfo.getSupplierzhname().trim() + "%%");
		}
		return hql;
	}

	
	public List<SupplierInfo> combobox(SupplierInfo SupplierInfo) {
		String hql = "from TSupplierInfo t ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(SupplierInfo, hql, params);
		hql += " order by name";
		List<TsupplierInfo> l = supplierInfoDao.find(hql, params, 1, 10);
		List<SupplierInfo> nl = new ArrayList<SupplierInfo>();
		changeModel(l, nl);
		return nl;
	}

	
	public void remove(String ids) {
		for (String id : ids.split(",")) {
			TsupplierInfo t = supplierInfoDao.get(TsupplierInfo.class, id);
			if (t != null) {
				supplierInfoDao.delete(t);
			}
		}
	}

	
	public SupplierInfo edit(SupplierInfo supplierInfo) {
		TsupplierInfo t = supplierInfoDao.get(TsupplierInfo.class, supplierInfo.getSupplierid());
		if (t != null) {
			
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("tsupplier", t);
			supplierVehicleDao.executeHql("delete TsupplierTvehicle t where t.tsupplier=:tsupplier", params);
			supplierInfoDao.saveOrUpdate(t);
			BeanUtils.copyProperties(supplierInfo, t);
				t.setCreatetime(new Date());
			BeanUtils.copyProperties(t, supplierInfo);
            
		if (supplierInfo.getVehicleIds()!= null) {
		    String vehicleIds = "";
		    String tons = "";
				
				for (String id : supplierInfo.getVehicleIds().split(",")) {
					TvehicleInfo v = vehicleDao.get(TvehicleInfo.class, id);
					if (v != null) {
						TsupplierTvehicle uv = new TsupplierTvehicle();
						uv.setId(UUID.randomUUID().toString());
						uv.setTvehicle(v);
						uv.setTsupplier(t);
						supplierVehicleDao.save(uv);
						System.out.println("关联成功！");
						if (vehicleIds.length() > 0) {
						    vehicleIds += ",";
						    tons += ",";
						}
						vehicleIds += v.getVehicleid();
						tons += v.getTon();
					}
				}
				supplierInfo.setVehicleIds(vehicleIds);
				supplierInfo.setTons(tons);
			}
			System.out.println(supplierInfo.getContact());
			return supplierInfo;
		}
		return supplierInfo;
	}

	
	public void modifyRole(SupplierInfo SupplierInfo) {
		/*for (String uid : SupplierInfo.getIds().split(",")) {
			TSupplierInfo u = SupplierInfoDao.get(TSupplierInfo.class, uid);
			if (u != null) {
				u.setModifydatetime(new Date());
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("tSupplierInfo", u);
				SupplierInfoRoleDao.executeHql("delete TSupplierInfoTrole t where t.tSupplierInfo=:tSupplierInfo", params);
				if (SupplierInfo.getRoleIds() != null) {
					for (String id : SupplierInfo.getRoleIds().split(",")) {
						TvehicleInfo r = roleDao.get(TvehicleInfo.class, id);
						if (r != null) {
							TSupplierInfoTrole ur = new TSupplierInfoTrole();
							ur.setId(UUID.randomUUID().toString());
							ur.setTrole(r);
							ur.setTSupplierInfo(u);
							SupplierInfoRoleDao.save(ur);
						}
					}
				}
			}
		}*/
	}

	
	public void modifyPwd(SupplierInfo SupplierInfo) {
		/*TSupplierInfo u = SupplierInfoDao.get(TSupplierInfo.class, SupplierInfo.getId());
		if (u != null) {
			u.setPwd(Encrypt.e(SupplierInfo.getPwd()));
		}*/
	}
}
